﻿#define Log
using UnityEngine;
using System.Collections;
using JSON;
namespace AssetBundleHelper{
	public class ABUpdate : MonoBehaviour {
		// Use this for initialization
		/// <summary>
		/// 游戏版本数据
		/// </summary>
		JSONNode gameVersion;
		/// <summary>
		/// 下一个版本号
		/// </summary>
		string nextVersion;
		string localversion;
		/// <summary>
		/// 发现新版本
		/// </summary>
		public System.Action<string,string> findNewVersionCallback;
		/// <summary>
		/// 下载回调
		/// </summary>
		public System.Action<WWW> downLoadCallback;
		/// <summary>
		/// 解压回调
		/// </summary>
		public System.Action<string,string,float> decompressionCallback;
		/// <summary>
		/// 更新完毕
		/// </summary>
		public System.Action updateFinish;

		public void Begin(){
			LoadGameVersion ();
		}

		void LoadGameVersion(){
			AssetsLoader.Instance.LoadText ("GameVersion.txt", delegate(string obj) {
				gameVersion = JSONNode.Parse(obj);
				CheckUpdate();
			});
		}

		void CheckUpdate(){
			var versionNode = gameVersion ["versions"];
			var version = versionNode[versionNode.Count-1].ToString();
			localversion = TempUnit.myVersion;
			if(version.Equals(localversion)){
				#if Log
				Debug.Log(string.Format("当前版本[{0}]是最新的[{1}]不需要更新",localversion,version));
				#endif
				UpdateFinish();
			}else{
				for(var i = 0;i < versionNode.Count - 1;i++){
					if (localversion.Equals (versionNode [i].ToString ())) {
						nextVersion = versionNode [i + 1].ToString ();
					}
				}
				if (nextVersion == null) {
					nextVersion = versionNode [0].ToString ();
				}
				var splocal = localversion.Split('.');
				var spnet = nextVersion.Split('.');
				var count = Mathf.Max(splocal.Length,spnet.Length);
				for(var i =0;i < count;i++){
					var spli = splocal.Length>i ? splocal[i]:"0";
					var spni = spnet.Length>i ? spnet[i]:"0";
					if(spni != spli){
						switch(i){
						case 0:
							UpdateBig();
							break;
						case 1:
							UpdateMiddle();
							break;
						case 2:
							UpdateMini();
							break;
						default:
							UpdateMini();
							break;
						}
						break;
					}
				}
				TempUnit.myVersion = version;
			}
		}

		void UpdateBig(){
			Debug.Log("大版本更新:需要到AppStore上进行更新");
			UpdateAssetBundle ();
		}

		void UpdateMiddle(){
			Debug.Log("中版本更新:更新资源包与代码");
			UpdateAssetBundle ();
		}

		void UpdateMini(){
			Debug.Log("小版本更新:更新资源包");
			UpdateAssetBundle ();
		}

		void HandleAssetBundleFinish(){
			Debug.Log (string.Format("更新{0}版本完毕 继续检查", nextVersion));
			TempUnit.myVersion = nextVersion;
			CheckUpdate ();
		}
		void HandleProcesscallback(string allsize,string nowsize,float precent){
			if (decompressionCallback != null)
				decompressionCallback (allsize, nowsize, precent);
	//		Debug.Log (nowsize + "/" + allsize + "  ::" + precent);
		}

		void UpdateAssetBundle(){
			if (findNewVersionCallback != null)
				findNewVersionCallback (localversion,nextVersion);
			AssetsLoader.Instance.LoadAssetBundleByVersion (nextVersion,downLoadCallback,HandleProcesscallback, HandleAssetBundleFinish);
		}

		void UpdateFinish(){
			Debug.Log ("更新结束");
			if (updateFinish != null)
				updateFinish ();
		}

	}
}